
import { _decorator, Component, Node, systemEvent, System, SystemEvent, CameraComponent, Camera, Touch, EventTouch, PhysicsSystem, RigidBody, v3, director, Graphics } from 'cc';
const { ccclass, property } = _decorator;


 
@ccclass('Click')
export class Click extends Component {
    
    @property(CameraComponent)
    camera:Camera=null!;
    onLoad(){
        
    }

    start () {
        // [3]
        systemEvent.on(SystemEvent.EventType.TOUCH_START,this.onTouchStart,this);
    }

    // update (deltaTime: number) {
    //     // [4]
    // }

    onTouchStart(touch:Touch,event:EventTouch){
        let touchPos=touch.getLocation();
        let ray=this.camera.screenPointToRay(touchPos.x,touchPos.y);
        
        if(PhysicsSystem.instance.raycastClosest(ray)){
            const res=PhysicsSystem.instance.raycastClosestResult;
            const hitNode=res.collider.node;
            if(hitNode.name.startsWith('Cube')){
                console.log('hit cube'+hitNode.uuid);
                let rigidBody=hitNode.getComponent(RigidBody);
                let x=Math.random()*300,
                y=Math.random()*300,
                z=Math.random()*300;
                rigidBody?.applyForce(v3(0,600,0)); 
            }
        } 
    }
}
